import useMemberStore from "../store/moduls/user"

const baseUrl = 'http://124.93.196.45:10001/'

const httpInterceptor = {
	invoke(options) {
		if (!options.url.startsWith('http')) {
			options.url = baseUrl + options.url
		}
		options.timeout = 10000

		options.header = {
			...options.header,
		}
		const token = uni.getStorageSync('token')
		if (token) {
			options.header.Authorization = token
		}
	}
}
uni.addInterceptor("request", httpInterceptor)
uni.addInterceptor("uploadFile", httpInterceptor)

export const http = (options) => {
	return new Promise((resolve, reject) => {
		uni.request({
			...options,
			success(res) {
				resolve(res.data)
				if (res.data.code === 401) {
					uni.showToast({
						title:'请先登录在操作'
					})
				    setTimeout(()=>{
						uni.navigateTo({
							url:"/subpages/login/login"
						})
					},500)
				    reject(res)
				}
			},
			fail(err) {
				reject(err)
			}
		})
	})
}